<script setup lang='ts'>
import { onActivated, ref, onDeactivated, onMounted } from "vue"
import { getBookShelfBanner, getBookChapterList } from "@/api/index"
import { useRouter } from "vue-router";
import { useCounterStore } from '../stores/counter'
import LoadingView from "@/components/LoadingView.vue"
import { showConfirmDialog } from 'vant';
const $router = useRouter()
const store = useCounterStore()
const currentBooks = ref<any>(JSON.parse(`${localStorage.getItem('books')}`) || [])
const bannerInfo = ref<any>()
const isLoading = ref<boolean>(false)
onMounted(async () => {
  // console.log('在首次挂载、以及每次从缓存中被重新插入的时候调用');

  let res = await getBookShelfBanner()
  bannerInfo.value = res.data.data.banners[0]

});
onDeactivated(() => {
  // console.log('在从 DOM 上移除、进入缓存、以及组件卸载时调用');
})
const showBanner = ref<boolean>(true)
const toBookContent = async (book: any) => {
  isLoading.value = true

  let bookChapterRes = await getBookChapterList(book.source_uuid || book.id || book.p_id)

  store.getChapter(bookChapterRes.data.data.catalog)
  isLoading.value = false
  $router.push({
    path: '/content', query: {
      source_uuid: book.source_uuid || book.id || book.p_id, content_uuid: book.content_uuid, title: book.title, index: book.currentChapter > 1 ? book.currentChapter - 1 : 0
    }
  })
}
const loop = ref<any>()
const touchstart = () => {
  clearTimeout(loop.value); //再次清空定时器，防止重复注册定时器

  loop.value = setTimeout(() => {

    console.log("长按了");
    showDel.value = true

  }, 1000);

}

const touchend = () => {
  clearTimeout(loop.value); //清空定时器，防止重复注册定时器
}
const showDel = ref<boolean>(false)
const delLocalBook = (book: any) => {
  console.log(book);
  showConfirmDialog({
    title: '删除',
    message:
      `您确认要删除“${book.title}”吗？`,
  })
    .then(() => {
      currentBooks.value = [...currentBooks.value.filter((o: any) => o != book)]
      localStorage.setItem('books', JSON.stringify(currentBooks.value))
      showDel.value = false
    })
    .catch(() => {
      showDel.value = false
    });

}

const cancleDel = () => {
  showDel.value = false
}
</script>

<template>
  <div>
    <LoadingView v-if="isLoading" />
    <div class="shelf" @click="cancleDel">
      <div class="shelf-title">
        <div class="title-lift">
          <span>书架</span>
          <img src="@/assets/imgs/向下箭头2.png" alt="">
        </div>
        <div class="title-right">
          <img src="@/assets/imgs/搜索1.png" alt="" @click.stop="$router.push('/search')">
          <img src="@/assets/imgs/定时2.png" alt="">
          <img src="@/assets/imgs/列表4.png" alt="">
        </div>
      </div>
      <div class="shelf-content">
        <div class="shelf-banner" v-if="showBanner && bannerInfo" @click.stop="toBookContent(bannerInfo)">
          <div class="banner-left">
            <img :src="bannerInfo.cover + '?imageView&thumbnail=240y336'" alt="">
          </div>
          <div class="banner-right">
            <div class="right-title">
              <span>{{ bannerInfo.title }}</span>
              <span>{{ bannerInfo.readInfo }}</span>
            </div>
            <div class="right-detail">
              <p>{{ bannerInfo.description }}</p>
            </div>
          </div>
          <div class="close">
            <img src="@/assets/imgs/叉1.png" alt="" @click.stop="showBanner = false">
          </div>
        </div>
        <div class="shelf-books">
          <ul class="list">
            <li v-for="book, index in currentBooks" :key="index" @click.stop="toBookContent(book)"
              @touchstart="touchstart" @touchend="touchend">
              <div class="book-msg">
                <img :src="book.cover" alt="">
                <img src="@/assets/imgs/叉叉1.png" alt="" class="del-book" @click.stop="delLocalBook(book)" v-if="showDel">
                <p>{{ book.title }}</p>
              </div>
              <span>读到{{ book.currentChapter > 0 ? book.currentChapter : 1 }}章</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.shelf {
  width: 100vw;
  height: 100%;
  padding: 5rem 15rem 0;

  .shelf-title {
    height: 50rem;
    display: flex;
    justify-content: space-between;

    .title-lift {
      display: flex;
      align-items: center;

      span {
        font-size: 17rem;
        color: #333;
      }

      img {
        width: 10rem;
        height: 10rem;
        margin-left: 5rem;
      }
    }

    .title-right {
      display: flex;
      align-items: center;

      img {
        width: 25rem;
        margin-left: 15rem;
      }
    }
  }

  .shelf-content {
    height: calc(100% - 50rem);
    overflow: auto;

    .shelf-banner {
      border: 1rem solid #eee;
      box-shadow: 0 4rem 5rem 1rem #eee;
      display: flex;
      padding: 10rem;

      .banner-left {
        img {
          width: 50rem;
          font-size: 0;
          display: block;
        }
      }

      .banner-right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-left: 10rem;

        .right-title {
          display: flex;
          justify-content: space-between;

          span {
            font-size: 13rem;
            color: #333;
            white-space: nowrap;

            &:first-child {
              font-weight: bold;
              width: 60%;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              text-overflow: ellipsis;
              overflow: hidden;
              -webkit-line-clamp: 1;
            }


            &:last-child {
              color: #fc1c1c;
              font-size: 12rem;

            }
          }
        }

        .right-detail {
          p {
            font-size: 12rem;
            color: #888;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            text-overflow: ellipsis;
            overflow: hidden;
            -webkit-line-clamp: 2;
          }
        }
      }

      .close {
        img {
          width: 20rem;
          margin-left: 5rem;
        }
      }
    }

    .shelf-books {
      margin-top: 20rem;

      ul.list {
        display: flex;
        flex-wrap: wrap;
        justify-content: left;

        li {
          margin-bottom: 10rem;
          margin-right: 22.5rem;
          width: 100rem;
          height: 200rem;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          position: relative;

          &:nth-child(3n) {
            margin-right: 0;
          }

          .book-msg {
            img {
              width: 100rem;
              height: 133rem;

              &.del-book {
                width: 20rem;
                height: 20rem;
                position: absolute;
                right: 3rem;
                top: 3rem;
              }
            }


            p {
              margin-top: 10rem;
              font-size: 13rem;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              text-overflow: ellipsis;
              overflow: hidden;
              -webkit-line-clamp: 2;
            }
          }

          span {
            font-size: 13rem;
          }


        }
      }
    }
  }
}
</style>
